
/*
 * 文件名：UserServiceImpl.java.
 * 描述：
 * 创建人: 龚洪亮
 * 创建时间：2017年8月29日
 */

package com.fiberhome.platform.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fiberhome.platform.mapper.RoleMapper;
import com.fiberhome.platform.mapper.UserMapper;
import com.fiberhome.platform.mapper.UserRoleMapper;
import com.fiberhome.platform.pojo.Role;
import com.fiberhome.platform.pojo.User;
import com.fiberhome.platform.pojo.UserExample;
import com.fiberhome.platform.pojo.UserRole;
import com.fiberhome.platform.pojo.UserRoleExample;


/**
 * @Description 
 * @author 龚洪亮
 * @version 2017年8月29日
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@Override
	public User findByNameAndPass(String username, String password) {
		UserExample example = new UserExample();
		example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
		List<User> users = userMapper.selectByExample(example);
		if (users.size()==0) {
			return null;
		}
		return users.get(0);
	}
	@Override
	public User findByName(String username) {
		UserExample example = new UserExample();
		example.createCriteria().andUsernameEqualTo(username);
		List<User> users = userMapper.selectByExample(example);
		if (users.size()==0) {
			return null;
		}
		return users.get(0);
	}
	
	@Override
	public Set<String> getRolesName(User user) {
		UserRoleExample userRoleExample = new UserRoleExample();
		userRoleExample.createCriteria().andUserIdEqualTo(user.getId().intValue());
		List<UserRole> roles = userRoleMapper.selectByExample(userRoleExample);
		
		
		Set<String> names = new HashSet<String>();
		
		for(UserRole r:roles) {
			Role selectByPrimaryKey = roleMapper.selectByPrimaryKey(r.getRoleId());
			names.add(selectByPrimaryKey.getRolename());
		}
		
		return names;
	}

}
